package com.lsa.letcode.the66.plusOne;

public class Solution {
    public int[] plusOne(int[] digits) {
    	int carry = 0;
    	for (int i = digits.length - 1; i > -1; i--) {
			int d = digits[i] + carry + (i == digits.length - 1? 1 : 0);
			carry = d / 10;
			d = d % 10;
			digits[i] = d;
		}
    	
    	int[] res;
    	if (carry != 0) {
    		res = new int[digits.length + 1];
    		for (int i = digits.length - 1; i > -1 ; i--) {
    			res[i + 1] = digits[i];
			}
    		res[0] = carry;
    	}
    	else {
    		res = digits;
    	}
    	return res;
    }
}